package leftMain;
//package org.dlsu.mt.parser;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import org.dlsu.mt.preprocessing.TestDocParsing;

public class LeftGUI extends JFrame implements MouseListener
{
	private LEFTTranslator	translator;	
	
	public static LeftGUI left;
	static boolean bViewDictionary = false;
	public static SearchWordGUI searchWordGUI;
	
	private boolean bChecker = false;
	private boolean bLogin = false;
	private JFileChooser chooser;
	private JFileChooser chooser2;
	
	private JMenuBar menuBar;
	private JSeparator menuSeparator, menuSeparator2, menuSeparator3;
	
	private JMenu menuFile;
	private JMenuItem menuFileNew;
	private JMenuItem menuFileLoad;
	private JMenu menuFileSave;
	private JMenuItem menuFileSaveSource;
	private JMenuItem menuFileSaveTarget;
	private JMenuItem menuFileSaveProject;
	private JMenuItem menuFileExit;
	
	private JMenu menuOption;
	private JMenu menuDictionary;
	private JMenuItem menuDictionaryView;
	private JMenuItem menuDictionarySearch;
	private JMenuItem menuRefresh;
	private JMenu menuDictionaryAdmin;
	private JMenuItem menuDictionaryAdminAdd;
	private JMenuItem menuDictionaryAdminEdit;
	private JMenuItem menuDictionaryAdminDelete;
	private JMenuItem menuLogin;
	private JMenuItem menuLogout;
	
	private JMenu menuHelp;
	private JMenuItem menuHelpHelp;
	private JMenuItem menuHelpAbout;
	
	private JLabel labelSource;
	private JLabel labelTarget;
	private JLabel label1;
	private JLabel label2;
	private JTextArea textSource;
	private JTextArea textTarget; 
	
	private int nSave = -1;
	
	private JButton translateButton;
	private JLabel labelTranslate;// labelTranslate2;
	private JComboBox comboBox; //comboBox2;
	private JScrollBar j;
	
	private JScrollPane scrollPaneSource;
	private JScrollPane scrollPaneTarget;
	
	private PrintWriter pw;
	
	public LeftGUI(LeftGUI l) throws Exception
	{
		
		SplashScreenMain splash;
		
		try{
      		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    	}
    	catch (Exception e) {
      	e.printStackTrace();
    	}
    	splash = new SplashScreenMain();
    	
    	pw = new PrintWriter("C:/log.txt");
		translator = new LEFTTranslator(pw);
		
		left = l;
		
		menuBar = new JMenuBar();
		menuSeparator = new JSeparator();
		menuSeparator2 = new JSeparator();
		menuSeparator3 = new JSeparator();
		
		menuFile = new JMenu();
		menuFileNew = new JMenuItem();
		menuFileLoad = new JMenuItem();
		menuFileSave = new JMenu();
		menuFileSaveSource = new JMenuItem();
		menuFileSaveTarget = new JMenuItem();
		menuFileSaveProject = new JMenuItem();
		menuFileExit = new JMenuItem();
		
		menuOption = new JMenu();
		menuDictionary = new JMenu();
		menuDictionaryAdmin = new JMenu();
		menuDictionaryAdminAdd = new JMenuItem();
		menuDictionaryAdminEdit = new JMenuItem();
		menuDictionaryAdminDelete = new JMenuItem();
		menuDictionaryView = new JMenuItem();
		menuDictionarySearch = new JMenuItem();
		menuRefresh = new JMenuItem();
		menuLogin = new JMenuItem();
		menuLogout = new JMenuItem();
		
		menuHelp = new JMenu();
		menuHelpHelp = new JMenuItem();
		menuHelpAbout = new JMenuItem();
		
		label1 = new JLabel();
		label2 = new JLabel();
		labelSource = new JLabel();
		labelTarget = new JLabel();
		textSource = new JTextArea();
		textTarget = new JTextArea();
		
		scrollPaneSource = new JScrollPane();
		scrollPaneTarget = new JScrollPane();
			
		labelTranslate = new JLabel();
		//labelTranslate2 = new JLabel();
		translateButton = new JButton();
		comboBox = new JComboBox();
		//comboBox2 = new JComboBox();
		
		//Translation Label - 1
		labelTranslate.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        labelTranslate.setText("Choose Translation:");
		labelTranslate.setBounds(5, 490, 160, 40);
		labelTranslate.setVisible(false);
		
		
		//Combobox - 1
		comboBox.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		comboBox.addItem("Please Choose . . .");
		comboBox.addItem("English to Filipino");
		comboBox.addItem("Filipino to English");
		//comboBox.setBounds(170, 490, 180, 40);
		comboBox.setBounds(5, 530, 180, 40);
		comboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxItemActionPerformed(evt);
            }
        });
       comboBox.setVisible(false);
        
		//Translate Button
		translateButton.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
        translateButton.setText("Translate");
		translateButton.setToolTipText("Translate");
		translateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt){
                translateButtonItemActionPerformed(evt);
            }
        });
        translateButton.setBounds(610, 530, 130, 40); 
        translateButton.setVisible(false);
             
		
		//Frame
		getContentPane().setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     	setSize(800, 640);
		setLocation(150, 20);
		this.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 14));
		setTitle("LFG Based English-Filipino Translator (LEFT)");
		getContentPane().setBackground(Color.LIGHT_GRAY);
		setCursor(Cursor.HAND_CURSOR);
		setResizable(false);
		setVisible(true);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				pw.close();
				dispose();
				System.exit(0);
			}
		});
		
		getContentPane().add(labelTranslate);
		getContentPane().add(comboBox);
		getContentPane().add(translateButton);	
		
		
		//Label for Language
		label1.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 16));
        label1.setText("English");
		label2.setFont(new java.awt.Font("Microsoft Sans Serif", 3, 16));
        label2.setText("Filipino");
                
        		
		//Label Input TextArea
		labelSource.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		labelSource.setText("Input Text:");
		labelSource.setBounds(5, 0, 90, 40);
		labelSource.setVisible(false);
		getContentPane().add(labelSource);
		
		//TextArea of Source Language
		textSource.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		textSource.setText("");
		textSource.setLineWrap(true);
		//textSource.addMouseListener(this);
		
		scrollPaneSource = new JScrollPane(textSource);
		scrollPaneSource.addMouseListener(this);
		scrollPaneSource.setBounds(5, 30, 380, 450);
		//scrollPaneSource.requestFocus();
		scrollPaneSource.setVisible(false);
		getContentPane().add(scrollPaneSource);
				
		
		//Label Output TextArea
		labelTarget.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		labelTarget.setText("Output Text:");
		labelTarget.setBounds(411, 0, 100, 40);
		labelTarget.setVisible(false);
		getContentPane().add(labelTarget);
		
		//TextArea of Target Language
		textTarget.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		textTarget.setText("");
		textTarget.setLineWrap(true);
		textTarget.setEditable(false);
		
		scrollPaneTarget = new JScrollPane(textTarget);
		scrollPaneTarget.addMouseListener(this);
		scrollPaneTarget.setBounds(411, 30, 380, 450);
		scrollPaneTarget.setVisible(false);
		getContentPane().add(scrollPaneTarget);
				
		
		//MenuBar
		menuBar.setBackground(Color.WHITE);
		menuBar.setSize(800,20);
		
		//File Menu
		menuFile.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFile.setForeground(Color.black);
		menuFile.setText("File");
		
		//File Menu - New
		menuFileNew.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileNew.setText("New");
		menuFileNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileNewItemActionPerformed();
            }
		});
		
		//File Menu - Load
		menuFileLoad.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileLoad.setText("Load");
		menuFileLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileLoadActionPerformed(evt);
            }
		});
		
				//File Menu - Save
		menuFileSave.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileSave.setText("Save");
		
		//File Menu - Save - Source
		menuFileSaveSource.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileSaveSource.setText("Save Source Document");
		menuFileSaveSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileSaveSourceActionPerformed(evt);
            }
		});
		
		//File Menu - Save Target
		menuFileSaveTarget.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileSaveTarget.setText("Save Target Document");
		menuFileSaveTarget.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileSaveTargetActionPerformed(evt);
            }
		});
		
		//File Menu - Save Project
		menuFileSaveProject.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileSaveProject.setText("Save Project");
		menuFileSaveProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileSaveProjectActionPerformed(evt);
            }
		});
		
		menuFileSave.add(menuFileSaveSource);
		menuFileSave.add(menuFileSaveTarget);
		menuFileSave.add(menuFileSaveProject);
		
		//File Menu - Exit
		menuFileExit.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuFileExit.setText("Exit");
		menuFileExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        
		menuFile.add(menuFileNew);
		menuFile.add(menuFileLoad);	
		menuFile.add(menuFileSave);
		menuFile.add(menuSeparator);
		menuFile.add(menuFileExit);
		
		//Option Menu
		menuOption.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuOption.setForeground(Color.black);
		menuOption.setText("Options");
		
		//Option Menu - Dictionary
		menuDictionary.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionary.setText("Dictionary");
		
		//Option Menu - Dictionary - View Dictionary
		menuDictionaryView.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionaryView.setText("View Dictionary");
		menuDictionaryView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDictionaryViewActionPerformed(evt);
            }
        });
        	
		menuDictionarySearch.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionarySearch.setText("Search Word");
		menuDictionarySearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDictionarySearchActionPerformed(evt);
            }
        });
        
		
		menuDictionary.add(menuDictionaryView);
		menuDictionary.add(menuDictionarySearch);
		menuDictionary.add(menuRefresh);
		
		//Option Menu - Admin Dictionary
		menuDictionaryAdmin.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionaryAdmin.setText("Administrator Functions");
		menuDictionaryAdminAdd.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionaryAdminAdd.setText("Add Dictionary Entry");
		
		//NEW CODE
		menuDictionaryAdminAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAddEntryItemActionPerformed(evt);
            }
        });
		////
		
		menuDictionaryAdminEdit.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionaryAdminEdit.setText("Edit Dictionary Entry");
		
		//NEW CODE
		menuDictionaryAdminEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditEntryItemActionPerformed(evt);
            }
        });
		////
		
		menuDictionaryAdminDelete.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuDictionaryAdminDelete.setText("Delete Dictionary Entry");
		
		//NEW CODE
		menuDictionaryAdminDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDeleteEntryItemActionPerformed(evt);
            }
        });
		////
		
		menuDictionaryAdmin.add(menuDictionaryAdminAdd);
		menuDictionaryAdmin.add(menuDictionaryAdminEdit);
		menuDictionaryAdmin.add(menuDictionaryAdminDelete);
		
		//Option Menu - Admin Login/Logout
		menuLogin.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuLogin.setText("Administrator Login");
		menuLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuLoginItemActionPerformed(evt);
            }
        });
		menuLogout.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuLogout.setText("Adminitrator Logout");
		menuLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuLogoutItemActionPerformed(evt);
            }
        });
		
		menuOption.add(menuDictionary);
		menuOption.add(menuSeparator3);
		menuOption.add(menuLogin);
		menuOption.add(menuDictionaryAdmin);
		menuDictionaryAdmin.setVisible(false);
    	menuOption.add(menuLogout);
    	menuLogout.setVisible(false);
		
		//Help Menu
		menuHelp.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuHelp.setText("Help");
		menuHelp.setForeground(Color.black);
		menuHelpHelp.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuHelpHelp.setText("Help");
		menuHelpAbout.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14));
		menuHelpAbout.setText("About Us");
		menuHelp.add(menuHelpHelp);
		menuHelp.add(menuSeparator2);
		menuHelp.add(menuHelpAbout);

		menuBar.add(menuFile);
		menuBar.add(menuOption);
		menuBar.add(menuHelp);
	
	
		//Adds and Updates to the frame the MenuBar
		setJMenuBar(menuBar);
		menuBar.updateUI();
		
		splash.splashScreenDestruct();
		
		//Dictionary.strLang = new String("Filipino");
	
	}

	public void mouseClicked(MouseEvent e)
	{		
	}
	public void mouseEntered(MouseEvent e)
	{
	}
	public void mouseExited(MouseEvent e)
	{
	}
	public void mousePressed(MouseEvent e)
	{
	}
	public void mouseReleased(MouseEvent e)
	{
	}
	
	private void menuDictionaryViewActionPerformed(java.awt.event.ActionEvent evt)
	{
		ViewDictionaryGUI view = new ViewDictionaryGUI(this.getX(), this.getY());
    }
    
    private void menuDictionarySearchActionPerformed(java.awt.event.ActionEvent evt)
	{
		searchWordGUI = new SearchWordGUI(this.getX(), this.getY());
    }
    
	private void menuItemItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		this.dispose();
    }
    
    //UPDATED CODE
	private void menuLoginItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		boolean login = false;
		//javax.swing.JOptionPane.showMessageDialog(this, "Prompts for the user name and password.", "Temporary!", javax.swing.JOptionPane.OK_OPTION);		
		String strName = javax.swing.JOptionPane.showInputDialog(this, "Enter name", "Administrator Log-in", 3);
		if (strName == null){
			javax.swing.JOptionPane.showMessageDialog(this, "Log-in unsuccessful.", "Administrator Log-in", javax.swing.JOptionPane.OK_OPTION);
			login = false;
		}
		else{
			
			String strPass = javax.swing.JOptionPane.showInputDialog(this, "Enter password", "Administrator Log-in", 3);	
			
			 if(!checkAdministratorLogin(strName, strPass))
			 	javax.swing.JOptionPane.showMessageDialog(this, "Invalid username/password", "Administrator Log-in", javax.swing.JOptionPane.ERROR_MESSAGE);
			 else{
			 	javax.swing.JOptionPane.showMessageDialog(this, "Welcome " + strName.toUpperCase(), "Administrator Log-in", 1);
			 	login = true;
			 }			 	
		}		
		
		if(login){
	    	this.menuLogin.setVisible(false);
	    	this.menuDictionaryAdmin.setVisible(true);
	    	this.menuLogout.setVisible(true);
	    }
	    
    	this.repaint();
    }
    
    private boolean checkAdministratorLogin(String name, String pass){
    	
    	boolean result = false;  
    	String msg = "";
    	int counter = 0	;
   		
   		try{
   			
   			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	        Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();
   			
			ResultSet rs = stmt.executeQuery("SELECT * FROM administrator WHERE name = '"+name+"' AND password = '"+pass+"'");
									
			try
			{
				while(rs.next())
					counter++;
			} catch ( SQLException e ) {}
			
			stmt.close();
			con.close();
			
			if(counter == 0)
				return false;
			else
				return true;
			
						
   		}catch(Exception e)
		{
			System.out.println("Error "+e.toString());
			msg = e.toString();
		}
		
		return false;
		
    }
    /////////////////////
    
    private void menuLogoutItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		javax.swing.JOptionPane.showMessageDialog(this, "Succesful logout.", "Administrator Log-out", javax.swing.JOptionPane.OK_OPTION);
    	this.menuLogout.setVisible(false);
    	this.menuDictionaryAdmin.setVisible(false);
    	//this.menuOption.add(menuLogin);
    	this.menuLogin.setVisible(true);
    	this.repaint();
    }
    
    //NEW CODE
    private void menuAddEntryItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		//javax.swing.JOptionPane.showMessageDialog(this, "Add Entry.", "Temporary!", javax.swing.JOptionPane.OK_OPTION);
		AddEntryGUI aeg = new AddEntryGUI(this.getX(), this.getY(), this.translator);
    	this.repaint();
    }
    
    private void menuEditEntryItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		//javax.swing.JOptionPane.showMessageDialog(this, "Edit Entry.", "Temporary!", javax.swing.JOptionPane.OK_OPTION);
		
		EditEntryGUI a = new EditEntryGUI(this.getX(), this.getY(), this.translator);
    	this.repaint();
    }
    
    private void menuDeleteEntryItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		//javax.swing.JOptionPane.showMessageDialog(this, "Delete Entry.", "Temporary!", javax.swing.JOptionPane.OK_OPTION);
		DeleteEntryGUI a = new DeleteEntryGUI(this.getX(), this.getY(), this.translator);
    	this.repaint();
    }
    //////////////
    
    
	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		pw.close();
		this.dispose();
    }
    
     private void translateButtonItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		if(textSource.getText().equals("Enter input text here . . ."))
			javax.swing.JOptionPane.showMessageDialog(this, "Please Enter text in Input Text.", "L.E.F.T.", javax.swing.JOptionPane.ERROR_MESSAGE);
		else if(textSource.getText().equals(""))
			javax.swing.JOptionPane.showMessageDialog(this, "Please Enter text in Input Text.", "L.E.F.T.", javax.swing.JOptionPane.ERROR_MESSAGE);
		else if(comboBox.getSelectedIndex() == 0)
			javax.swing.JOptionPane.showMessageDialog(this, "Please Choose Direction of Translation.", "L.E.F.T.", javax.swing.JOptionPane.ERROR_MESSAGE);
    	else
    		{
    			try
    			{
    				    				    				
    				textTarget.setText("");
    				Scanner scan = new Scanner();
    				int l = 0;
    				
    				if(comboBox.getSelectedIndex() == 1)
    				{
    					scan.cLang = '1';
    					scan.scan(this, textSource.getText().toString(), translator);
    					while(l < scan.sentences.size())
    					{    			
    						if(scan.acceptedSentences.get(l) == true)
    							textTarget.append(translator.translate1To2(scan.sentences.get(l).toString()) + scan.punctuation.get(l));
    						else
    							textTarget.append(scan.sentences.get(l).toString() + scan.punctuation.get(l) );
    						l++;
    						if(l != scan.sentences.size())
    							textTarget.append(" ");
    					}
    				}
    				else
    				{
    					scan.cLang = '2';
    					scan.scan(this, textSource.getText().toString(), translator);
    					while(l < scan.sentences.size())
    					{
    						if(scan.acceptedSentences.get(l) == true)
    							textTarget.append(translator.translate2To1(scan.sentences.get(l).toString()) + scan.punctuation.get(l));
    						else
    							textTarget.append(scan.sentences.get(l).toString() + scan.punctuation.get(l) );
    						l++;
    						if(l != scan.sentences.size())
    							textTarget.append(" ");
    					}
    					
    				}
    				
    			}
    			catch(Exception w)
    			{
    				System.out.println("LeftGUI : " + w.getMessage());
    			}
    		}
    }
    
    
    private void comboBoxItemActionPerformed(java.awt.event.ActionEvent evt)
	{
		if(comboBox.getSelectedIndex() == 1)
		{
			this.label1.setBounds(100, 0, 90, 40);
			this.label1.setVisible(true);
			this.getContentPane().add(label1);
			this.label2.setBounds(516, 0, 90, 40);
			this.label2.setVisible(true);
			this.getContentPane().add(label2);
			this.repaint();
		}
		else if(comboBox.getSelectedIndex() == 2)
		{
			this.label2.setBounds(100, 0, 90, 40);
			this.label2.setVisible(true);
			this.getContentPane().add(label2);
			this.label1.setBounds(516, 0, 90, 40);
			this.label1.setVisible(true);
			this.getContentPane().add(label1);
			this.repaint();
		}
		else if(comboBox.getSelectedIndex() == 0)
		{
			this.label1.setVisible(false);
			this.label2.setVisible(false);
			this.repaint();
		}
    }    
    
    private void menuFileNewItemActionPerformed()
	{
		if(bChecker != false)
		{
			if(textSource.getText().equals("")|| textSource.getText().equals("Enter input text here . . ."))
			{
			}
			else
			{
				Object j = javax.swing.JOptionPane.showConfirmDialog(this, "Do you want to save your previous work?", "Alert", javax.swing.JOptionPane.YES_NO_OPTION);
				if(j.toString().equals("0"))
				{
					javax.swing.JOptionPane.showMessageDialog(this, "Saving work file.", "Saving File ...", javax.swing.JOptionPane.OK_OPTION);
				}
			}
			this.textSource.setText("");
			this.textTarget.setText("");
			this.comboBox.setSelectedIndex(0);		
			
		}
		else
		{
					
			this.labelSource.setVisible(true);
			this.labelTarget.setVisible(true);			
			this.scrollPaneSource.setVisible(true);
			this.scrollPaneTarget.setVisible(true);
			this.labelTranslate.setVisible(true);
			this.comboBox.setVisible(true);
			this.translateButton.setVisible(true);
						
			this.repaint();
			bChecker = true;
		}
		
	}
	
	private void menuFileLoadActionPerformed(java.awt.event.ActionEvent evt)
	{
		chooseFile();
	}
		
	private void chooseFile()
	{
		String name = "";
		chooser = new JFileChooser();
		
		ExampleFileFilter filter1 = new ExampleFileFilter();
		ExampleFileFilter filter2 = new ExampleFileFilter();
			
	    filter1.addExtension("doc");
	    filter2.addExtension("txt");
	    
		filter1.setDescription("Word Document");
		filter2.setDescription("Text File");
		
		chooser.setFileFilter(filter1);
		chooser.setFileFilter(filter2);
		chooser.setAcceptAllFileFilterUsed(false);
		
	    int returnVal = chooser.showOpenDialog(left);
	    
	    try{
	    
	    	name = chooser.getSelectedFile().getName();
	    }catch(Exception e){
	    	return;
	    }
	    	    
	    if(returnVal == JFileChooser.APPROVE_OPTION)
	    {
	    	if(name.endsWith("txt"))
	    		textReader();
	    	else if(name.endsWith("doc"))
	    		docReader();
	    	
	    	//menuFileNewItemActionPerformed();
	    	this.labelSource.setVisible(true);
			this.labelTarget.setVisible(true);			
			this.scrollPaneSource.setVisible(true);
			this.scrollPaneTarget.setVisible(true);
			this.labelTranslate.setVisible(true);
			this.comboBox.setVisible(true);
			this.translateButton.setVisible(true);
						
			this.repaint();
			bChecker = true;
	   
	    }
	}
	
	public void docReader()
	{
		String textmessage = "";
		String filename = "" + chooser.getSelectedFile().getAbsolutePath();
		
		try{
		
			TestDocParsing tdp = new TestDocParsing();
			this.textSource.setText(tdp.parseWordDocument(filename));
		}catch(Exception e){
			System.out.println("LOADING UNSUCCESSFUL! " + e.getMessage());
		}
	}
	
	
	public void textReader()
	{
		String textmessage = "";
		String filename = "" + chooser.getSelectedFile().getAbsolutePath();
		try
		{
			//System.out.println(" -----> " + chooser.getSelectedFile().getCanonicalPath());
			BufferedReader in = new BufferedReader (new FileReader (filename));
			String line;
			
			while ((line = in.readLine()) != null)
			{					
				textmessage = textmessage + line;
				textmessage = textmessage + "\n";
			}
			
			this.textSource.setText(textmessage);
			in.close();
		}
		catch (IOException ex1)
		{
		  	
      		System.err.println("IO Exception (main): " + ex1.getMessage());
       	}	
	}
	
	private void menuFileSaveSourceActionPerformed(java.awt.event.ActionEvent evt)
	{
		nSave = 0;
		saveFile();
	}
	
	private void menuFileSaveTargetActionPerformed(java.awt.event.ActionEvent evt)
	{
		nSave = 1;
		saveFile();
	}
	
	private void menuFileSaveProjectActionPerformed(java.awt.event.ActionEvent evt)
	{
		saveProject();
	}
	
	private void saveFile()
	{
		chooser2 = new JFileChooser();
		chooser2.setDialogType(1);
		
		ExampleFileFilter filter1 = new ExampleFileFilter();
		ExampleFileFilter filter2 = new ExampleFileFilter();	
			
	    filter1.addExtension("doc");
	    filter2.addExtension("txt");
	    
		filter1.setDescription("Word Document");
		filter2.setDescription("Text File");
	
		chooser2.setFileFilter(filter1);
		chooser2.setFileFilter(filter2);
		chooser2.setAcceptAllFileFilterUsed(false);
		
		int returnVal = chooser2.showSaveDialog(left);

	    if(returnVal == JFileChooser.APPROVE_OPTION)
	    {
	    	save();
	    }
	    
	}
	
	private void save()
	{
		String absoluteFilePath;
	
		if(chooser2.getFileFilter().getDescription().equals("Text File (.txt)"))
			absoluteFilePath = new String(chooser2.getSelectedFile() + ".txt");
		else
			absoluteFilePath = new String(chooser2.getSelectedFile() + ".doc");
	
		try
		{
			File outputFile = new File(absoluteFilePath);
			PrintWriter out = new PrintWriter(new FileWriter(outputFile));
			
			int nLen;
			char[] c;
		
			if(nSave == 0)
			{
				c = textSource.getText().toCharArray();
				nLen = textSource.getText().toString().length();
			}
			else
			{
				c = textTarget.getText().toCharArray();
				nLen = textTarget.getText().toString().length();
			}
			
			int y = 0;
			while(y < nLen)
			{
				if(c[y] == '\n')
					out.println();
				else
					out.print(c[y]);
				y++;
			}
			
			nSave = -1;
			out.close();
		
		}
		catch (IOException ex1)
		{
      		System.err.println("IO Exception (main): " + ex1.getMessage());
       		
       	}
       			
	}
	
	private void saveProject()
	{
		nSave = 0;
		saveFile();
		nSave = 1;
		saveFile();		
	}

}
